const App = getApp();
import {
  getArea
} from '../../../http/game'
let pageType = ''
Page({

  data: {
    city: [],
    Array: [],
    config: {
      horizontal: true, // 第一个选项是否横排显示（一般第一个数据选项为 热门城市，常用城市之类 ，开启看需求）
      animation: true, // 过渡动画是否开启
      search: true, // 是否开启搜索
      searchHeight: 45, // 搜索条高度
      suctionTop: true // 是否开启标题吸顶
    }
  },
  onLoad(options) {
    pageType = options.type || ''
    this.setData({
      source: options.source
    })
    this.distance(39.928712, 116.393345, 39.928722, 116.393853)
    // 获取球场列表
    getArea({}).then((res) => {
      console.log(wx.getStorageSync('location'));
      const areaList = res.data
      console.log(areaList);
      for (let i in areaList) {
        for (let n in areaList[i]) {
          for (let k in areaList[i][n].city_list) {
            for (let o in areaList[i][n].city_list[k].court_list) {
              areaList[i][n].city_list[k].court_list[o].distance = this.distance(wx.getStorageSync('location').lat, wx.getStorageSync('location').lng, areaList[i][n].city_list[k].court_list[o].course_latitude, areaList[i][n].city_list[k].court_list[o].course_longitude).toFixed(2)
            }
          }
        }
      }
      this.setData({
        city: areaList
      })
      console.log(this.data.city);
    })
  },
  bindtap(e) {
    wx.setStorageSync('areaname', e.detail.name);
    wx.setStorageSync('areacode', e.detail.code);
  },



  distance(la1, lo1, la2, lo2) {

    console.log(la1, lo1, la2, lo2);
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = Math.round(s * 10000) / 10000;
    console.log(s);
    return s;

  },
  // 选择球场后触发
  pickerChange(course) {
    if (pageType === 'create') {
      const pages = getCurrentPages();
      const prevPage = pages[pages.length - 2]
      prevPage.setData({
        form_course: course.detail
      })
      wx.navigateBack();
      return
    }
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; //上一个页面
    console.log(course.detail)
    console.log(prevPage, 'prevPage')

    prevPage.onShow(course.detail);
    wx.navigateBack();
  }
})